package model

import (
	"gin-worked/dao"
)

type System struct {
	Id         int    `json:"id"`
	Username   string `gorm:"column(username)" json:"username"`
	Nickname   string `gorm:"column(nickname)" json:"nickname"`
	Headimg    string `gorm:"column(headimg)" json:"headimg"`
	Tel        string `gorm:"column(tel)" json:"tel"`
	Password   string `gorm:"column(password)" json:"password"`
	Status     string `gorm:"column(status)" json:"status"`
	Salt       string `gorm:"column(salt)" json:"salt"`
	DeleteTime string `gorm:"column(delete_time)" json:"delete_time"`
	CreateTime string `gorm:"column(create_time)" json:"create_time"`
	UpdateTime string `gorm:"column(update_time)" json:"update_time"`
}

// GetSystemInfo 获取管理员信息
func GetSystemInfo(data *System) (System, error) {
	var user System
	err := dao.Db.Where(data).First(&user).Error

	return user, err
}

// SystemAdd 添加管理员信息
func SystemAdd(data *System) (int, error) {
	err := dao.Db.Create(&data).Error

	return data.Id, err
}

// SystemList 获取用户列表
func SystemList(data *System, field []string, order string, offset int, limit int) ([]System, error) {
	var user []System
	err := dao.Db.Select(field).Where(data).Offset(offset).Limit(limit).Order(order).Find(&user).Error
	return user, err
}

func SystemTotal(data *System) int64 {
	var count int64
	dao.Db.Model(&System{}).Where(data).Count(&count)

	return count
}

func SystemEdit(data *System, content map[string]interface{}) error {
	err := dao.Db.Model(&data).Updates(content).Error
	return err

}
